package com.wudi.estate.service.impl.parkingmanagement;

import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.bean.parkingmanagement.Parking;
import com.wudi.estate.mapper.communitymanagement.CommunityMapper;
import com.wudi.estate.mapper.parkingmanagement.ParkingMapper;
import com.wudi.estate.service.parkingmanagement.ParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ParkingServiceImpl implements ParkingService {
    @Autowired
    private ParkingMapper parkingMapper;
    @Autowired
    private CommunityMapper communityMapper;

//    查询所有车位包括车位对应的小区（用于显示车位列表）
    @Override
    public List<Map<String, Object>> selectAll() {
        List<Map<String,Object>> lists = new ArrayList<>();
        List<Parking> parkings = parkingMapper.selectList(null);
        for (Parking parking : parkings) {
            Map<String ,Object> maps =new HashMap<>();
            maps.put("parkingById",parking);
            Community community = communityMapper.selectById(parking.getCid());
            maps.put("communityByCid",community);
            lists.add(maps);
        }
        return lists;
    }

    @Override
    public void insert(Parking parking) throws Exception {
        parkingMapper.insert(parking);
    }

    @Override
    public Map<String, Object> selectParkingAndCommunity(String id) {
        Parking parking = parkingMapper.selectById(id);
        List<Community> communities = communityMapper.selectList(null);
        Map<String,Object> maps = new HashMap<>();
        maps.put("parkingById",parking);
        maps.put("communities",communities);
        return maps;
    }

    @Override
    public void update(Parking parking) throws Exception {
        parkingMapper.updateById(parking);
    }
//删除信息
    @Override
    public void delete(String id) throws Exception {
        Parking parking = parkingMapper.selectById(id);
        parking.setState(false);
        parkingMapper.updateById(parking);
    }
}
